# coding=utf-8
# Copyright (c) 2020. ItsukiFujii.

import re   #re是正则表达式相关的包
import collections#容器数据类型相关包，

list_formula = []#定义了4个列表
list_terms_left = []
list_terms_right = []
list_res = []
dict_atoms_left = collections.defaultdict(int)#defaultdict字典的子类，提供了一个工厂函数，为字典查询提供一个默认值
dict_atoms_right = collections.defaultdict(int)


# 计算每个化合物
def fk_it(formula):#使用def定义一个函数
    stack = list()
    stack.append(collections.Counter())
    #append方法是向列表末端添加元素
    #一个 Counter 是一个 dict 的子类，用于计数可哈希对象。它是一个集合，元素像字典键(key)一样存储，它们的计数存储为值。计数可以是任何整数值，包括0和负数。
    found = re.findall(r'([A-Z][a-z]?)(\d*)|(\()|(\))(\d*)', formula)#函数传入的字符串，即化学方程式
    for name, n_atom, open_paren, close_paren, coef in found:
        if name:
            stack[-1][name] += int(n_atom or 1)
        if open_paren:
            stack.append(collections.Counter())
        if close_paren:
            top = stack.pop()
            for key in top.keys():
                top[key] *= int(coef or 1)
            stack[-1] += top
    return dict(stack.pop())


# 处理每个方程
def handle_equation(terms_all, dict_atoms):
    for term in terms_all:
        coef_term = re.match(r'\d+', term)
        coef_term = 1 if coef_term is None else int(coef_term.group())
        tmp = fk_it(term)
        for key in tmp.keys():
            tmp[key] *= coef_term
        for k, v in tmp.items():
            if str(dict_atoms.keys()).find(k) != -1:
                dict_atoms[k] += v
            else:
                dict_atoms.update({k: v})


if __name__ == '__main__':
    num_of_formula = int(input())
    for i in range(num_of_formula):
        list_formula.append(input())
        list_res.append('Y')

    # 分解方程式
    for item in list_formula:
        list_terms_left.append(item[:item.find('=')].split('+'))
        list_terms_right.append(item[item.find('=') + 1:].split('+'))

    # 判断双方原子数是否相等
    cntr_tmp = 0
    for terms_all_left, terms_all_right in zip(list_terms_left, list_terms_right):
        dict_atoms_left.clear()
        dict_atoms_right.clear()

        # left part
        handle_equation(terms_all_left, dict_atoms_left)
        # right part
        handle_equation(terms_all_right, dict_atoms_right)
        # 比较数字
        if dict_atoms_left != dict_atoms_right:
            list_res[cntr_tmp] = 'N'

        # print(dict_atoms_left, dict_atoms_right, list_res[cntr_tmp])  # for debug
        print(list_res[cntr_tmp])
        cntr_tmp += 1